package base.sound
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	
	import utils.ApplicationDomainUtil;
	
	public class SoundBase extends EventDispatcher
	{
		private var _id:String;
		private var _className:String;
		private var _loop:int;
		private var _soundChannel:SoundChannel;
		private var _soundAsset:*;
		private var _sound:Sound;
		private var _volumn:int;
		private var _position:Number=0;
		private var _isPause:Boolean=false;
		
		/**
		 *声音类 
		 * @param className 资源类名
		 * @param loop 循环次数,-1为无限循环
		 * @param volumn 音量大小
		 * 
		 */		
		public function SoundBase(className:String, loop:int=0, volumn:int=100)
		{
			_className=className;
			_loop=loop;
			_volumn=volumn;
			_soundAsset=ApplicationDomainUtil.getDefinitionByName(_className);
			if(!_soundAsset)return;
			_sound=new _soundAsset() as Sound;
		}
		
		public function play():void
		{
			stop();//当播放时，会创建新的SoundChannel，所以要先清除旧的资源
			_position=0;
			_soundChannel = _sound.play(_position, 0, new SoundTransform(_volumn/100));
			if(!_soundChannel) return;//如果没装声卡"_soundChannel"对象会为空
			_soundChannel.addEventListener(Event.SOUND_COMPLETE, __playComplete);
		}
		
		public function stop():void
		{
			if(!_soundChannel) return;
			_soundChannel.removeEventListener(Event.SOUND_COMPLETE, __playComplete);
			_soundChannel.stop();
			_soundChannel=null;
		}
		
		public function pause():void
		{
			if(!_soundChannel) return;
			_isPause=true;
			_position=_soundChannel.position;
			stop();
		}
		
		public function continuePlay():void
		{
			stop();//当播放时，会创建新的SoundChannel，所以要先清除旧的资源
			_soundChannel = _sound.play(_position, 0, new SoundTransform(_volumn/100));
			if(!_soundChannel) return;
			_soundChannel.addEventListener(Event.SOUND_COMPLETE, __playComplete);
		}
		
		/**
		 *声音播放完毕时 
		 * @param evt
		 * 
		 */		
		private function __playComplete(evt:Event):void
		{
			if(_loop<0)
			{//无限循环
				play();
			}
			else
			{
				if(_loop==0)
				{//循环次数结束
					var sc:SoundChannel = evt.currentTarget as SoundChannel;
					sc.removeEventListener(Event.SOUND_COMPLETE, __playComplete);
					sc.stop();
					sc=null;
					dispatchEvent(new SoundEvent(SoundEvent.PLAY_END));
				}
				else
				{
					_loop--;
					play();
				}
			}
		}
		
		public function get volumn():int
		{
			return _volumn;
		}
		
		public function set volumn(value:int):void
		{
			_volumn = value;
			if(!_soundChannel) return;
			_soundChannel.soundTransform.volume=_volumn/100;
		}
		
		public function dispose():void
		{
			_soundAsset=null;
			if(_soundChannel)
			{
				_soundChannel.removeEventListener(Event.SOUND_COMPLETE, __playComplete);
				_soundChannel.stop();
			}
			_soundChannel=null;
			_sound=null;
			SoundFactory.Instance.deleteSound(_className);
		}
		
		public function get id():String
		{
			return _id;
		}
		
		public function set id(value:String):void
		{
			_id = value;
		}

		public function get className():String
		{
			return _className;
		}

	}
}